#include "drv_server.h"

#include "drv_server_type.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

uint8_t LoginHandler(uint8_t *body, uint16_t size);
uint8_t DisconHandler(uint8_t *body, uint16_t size);
uint8_t P2pHandler(uint8_t *body, uint16_t size);
uint8_t GroupHandler(uint8_t *body, uint16_t size);
uint8_t GroupReqHandler(uint8_t *body, uint16_t size);

/**
 * @brief  服务器的消息处理函数
 * @note
 * @retval None
 */
Server_Handler_Struct server_handlers[] = {
    {LOGIN_RESP, LoginHandler}, {DISCON_REQ, DisconHandler},
    {P2P_REQ, P2pHandler},      {GROUP_REQ, GroupReqHandler},
    {GROUP_RESP, GroupHandler},  // group msg前面带了4字节的发送方id
};

uint8_t ServerHandler(uint8_t type, uint8_t *data, uint16_t len) {
  int listnum = sizeof(server_handlers) / sizeof(server_handlers[0]);
  for (int i = 0; i < listnum; i++) {
    if (type == server_handlers[i].t) {
      //执行回调
      server_handlers[i].handle(data, len);
    }
  }
  return 0;
}

/************************************************************/
/*****************************回复*******************************/
/************************************************************/
/**
 * @brief  登录请求的结果
 * @note
 * @param  status:
 * @retval None
 */
// __attribute__((weak)) void User_LoginHandler(uint8_t status) {
//   if (status > 0) {
//     //成功
//     return;
//   }
//   //失败
//   return;
// };
/**
 * @brief  主动离线请求结果
 * @note
 * @param  status:
 * @retval None
 */
// __attribute__((weak)) void User_DisconHandler(uint8_t status) {
//   if (status > 0) {
//     //成功
//     return;
//   }
//   //失败
//   return;
// };
/**
 * @brief  设备到设备或者服务器到设备的用户消息
 * @note
 * @param  *body:
 * @param  size:
 * @retval None
 */
// __attribute__((weak)) void User_P2pAndGroupHandler(uint8_t *body,
//                                                    uint16_t size) {
//   return;
// };

__attribute__((weak)) void User_GroupReqHandler(uint8_t *body, uint16_t size) {
  return;
};
/******************************************************/
/******************************************************/
/******************************************************/
uint8_t LoginHandler(uint8_t *body, uint16_t size) {
  char *temp;
  body[size] = 0;

  if ((temp = strstr((const char *)body, ":")) != NULL) {
    //"code":"1"
    uint8_t code = *(temp + 1) - 0x30;
    printf("LoginHandler %d\n", code);
    if (code == SERVER_SUCCESS) {
      User_LoginHandler(1);
      return 1;
    }
    User_LoginHandler(0);
  }
  return 0;
}
uint8_t DisconHandler(uint8_t *body, uint16_t size) {
  char *temp;
  if ((temp = strstr((const char *)body, ":")) != NULL) {
    //"code":"1"
    uint8_t code = *(temp + 1) - 0x30;
    if (code == SERVER_SUCCESS) {
      User_DisconHandler(1);
      return 1;
    }
    User_DisconHandler(0);
  }
  return 0;
}
/**
 * @brief  设备到设备，或者服务器到设备
 * @note
 * @param  *body:
 * @param  size:
 * @retval
 */
uint8_t P2pHandler(uint8_t *body, uint16_t size) {
  User_P2pAndGroupHandler(body + 4, size - 4);
  return 0;
}
/**
 * @brief  服务器群发到设备
 * @note
 * @param  *body:
 * @param  size:
 * @retval
 */
uint8_t GroupHandler(uint8_t *body, uint16_t size) {
  User_P2pAndGroupHandler(body + 4, size - 4);
  return 0;
}

uint8_t GroupReqHandler(uint8_t *body, uint16_t size) {
  User_P2pAndGroupHandler(body, size);
  return 0;
}

/************************************************************/
/*****************************请求*******************************/
/************************************************************/
uint8_t HearthBeatReq(CMD_Packer *cmd) {
  uint8_t temp[1];
  DRV_Pack(cmd, temp, 0, HEART_BEAT_REQ);
  return 1;
}
uint8_t LoginReq(CMD_Packer *cmd, uint8_t *user, uint8_t usize, uint8_t *pswd,
                 uint8_t psize) {
  uint8_t temp[128];
  memset(temp, 0, 128);
  int len = sprintf(temp, "{\"loginname\":\" %s \",\"password\":\" %s \"}",
                    user, pswd);
  printf("LoginReq %s\n", temp);
  DRV_Pack(cmd, temp, len, DEVICE_LOGIN_REQ);
  return 1;
}
uint8_t DisconReq(CMD_Packer *cmd) {
  uint8_t temp[1];
  DRV_Pack(cmd, temp, 0, DISCON_REQ);
  return 1;
}

/**
 * @brief  设备到设备
 * @note
 * @param  *cmd:
 * @param  id:  目标ID
 * @param  *body:
 * @param  size:
 * @retval
 */
uint8_t P2pReq(CMD_Packer *cmd, int id, uint8_t *body, uint16_t size) {
  //注意这里的长度可能不够
  uint8_t temp[128];
  if (size > 124) return 0;
  memcpy(temp, &id, 4);
  memcpy(temp + 4, body, size);
  DRV_Pack(cmd, temp, size + 4, P2P_REQ);
  return 1;
}

/**
 * @brief  设备到服务器
 * @note
 * @param  *cmd:
 * @param  *body:
 * @param  size:
 * @retval
 */
uint8_t GroupReq(CMD_Packer *cmd, uint8_t *body, uint16_t size) {
  DRV_Pack(cmd, body, size, GROUP_REQ);
  return 1;
}
